package ru.fsd.sokoban.gui;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JPanel;
import ru.fsd.sokoban.GameBoard;


public class GameBoardPanel extends JPanel {
	
	private static final long serialVersionUID = 1L;
	private GameBoard board = null;
	private int cellSize = 32;
	
	public GameBoardPanel(GameBoard board) {
		this.board = board;
		setLayout(new GridLayout());
		updateSize();
	}
	
	public void setGameBoard(GameBoard board) {
		this.board = board;
		setLayout(new GridLayout());
		updateSize();
		repaint();
	}
	
	public GameBoard getGameBoard() {
		return board;
	}

	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		paintBoard(g2);
	}
	
	
	private void paintBoard(Graphics2D g) {
		for(int i = 0; i < board.getLengthByX(); i ++)
			for(int j = 0; j < board.getLengthByY(); j++)
				g.drawImage(board.getGameObject(i, j).getImage(), null, i*cellSize, j*cellSize);
		g.drawImage(board.getPlayer().getImage(), null, board.getPlayer().getX()*cellSize,board.getPlayer().getY()*cellSize);
	}
	
	public int getCellSize() {
		return cellSize;
	}
	
	public void setCellSize(int newCellSize) {
		this.cellSize = newCellSize;
	}
	
	public void updateSize() {
		int width = cellSize * board.getLengthByX();
		int height = cellSize * board.getLengthByY();
		setPreferredSize(new Dimension(width, height));
	}
}
